﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Script.Serialization;
using RBS.Core.Extension;
using RBS.Core.Model;
using RBS.Web.API.Models;

namespace RBS.Web.API.Areas.Dict.Controllers
{
    /// <summary>
    /// 数据字典API
    /// </summary>
    public class SelectDictController : ApiController
    {
          private ApiContext db = new ApiContext(); 
  
    
        /// <summary>
        /// 获取数据字典
        /// </summary>
        /// <param name="type">info 获取当前数据字典，child 获取当前数据字典子集</param>
        /// <param name="dictid"></param>
        /// <returns></returns>
          
        public JsonData Get(string type, int dictid)
        { 
            var jsonData = new JsonData();
            jsonData.Success = true;
            try
            {
                switch (type)
                {
                    case "info":
                       /* var date =
                            db.DictInfo.Where(c => c.DictID == dictid)
                              .Select(c => new {c.DictID, c.DictCode, c.DictName});*/
                        jsonData.Data = db.DictInfo.Where(c => c.DictID == dictid)
                                          .Select(c => new {c.DictID, c.DictCode, c.DictName}); //new JavaScriptSerializer().Serialize(db.DictInfo.Where(c => c.DictID == dictid).Select(c => new { c.DictID, c.DictCode, c.DictName }));
                        break;
                    case "child":
                        jsonData.Data =  db.DictInfo.Where(d => d.ParentID == dictid).Select(c=> new{c.DictID,c.DictCode,c.DictName});
                        break;
                    case "allchild":
                        jsonData.Data = SelectChildByDictId(dictid);
                        break; 
                    default:
                        jsonData.Data = "";
                        break;
                } 
            }
            catch (Exception ex)
            {
                jsonData.Success = false;
                jsonData.Error = ex.Message;
            }
            return jsonData; 
        }


        public JsonData Get(string type, string  dictcode)
        {
            var jsonData = new JsonData();
            jsonData.Success = true;
            try
            {
                switch (type)
                {
                    case "info":
                        /* var date =
                             db.DictInfo.Where(c => c.DictID == dictid)
                               .Select(c => new {c.DictID, c.DictCode, c.DictName});*/
                        jsonData.Data = db.DictInfo.Where(c => c.DictCode == dictcode)
                                          .Select(c => new { c.DictID, c.DictCode, c.DictName }); //new JavaScriptSerializer().Serialize(db.DictInfo.Where(c => c.DictID == dictid).Select(c => new { c.DictID, c.DictCode, c.DictName }));
                        break;
                    case "child":
                        jsonData.Data = db.DictInfo.Where(c => c.ParentID == db.DictInfo.FirstOrDefault(d => d.DictCode==dictcode).DictID).Select(c => new { c.DictID, c.DictCode, c.DictName });
                        break;
                    case "allchild":
                        // jsonData.Data = SelectChildByDictId(dictid);
                        break;
                    default:
                        jsonData.Data = "";
                        break;
                }
            }
            catch (Exception ex)
            {
                jsonData.Success = false;
                jsonData.Error = ex.Message;
            }
            return jsonData;
        }
 
        /// <summary>
        /// 获取数据字典子集
        /// </summary>
        /// <param name="dictid"></param>
        /// <returns></returns>
        private object SelectChildByDictId(int dictid)
        {  
            var clients = db.DictInfo.Where(c => c.ParentID == dictid)
                            .Select(c => new
                                {
                                    c.DictID,
                                    c.DictCode,
                                    c.DictName,
                                    Child = db.DictInfo.Where(dc => dc.ParentID == c.DictID)
                                }).ToList();
            return clients;
        } 
    }
}
